package com.hnucm.shopapp.ui.login;

import android.util.Log;

import com.hnucm.shopapp.bean.BaseBean;
import com.hnucm.shopapp.bean.User;

import java.util.List;

import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import okhttp3.RequestBody;

public class LoginPresenter implements LoginContract.ILoginPresenter {

    private LoginContract.ILoginView loginView;
    private LoginContract.ILoginModel loginModel;

    public LoginPresenter(LoginContract.ILoginView loginView) {
        this.loginView = loginView;
        loginModel = new LoginModel();
    }


    @Override
    public void getUserByAccount(RequestBody body) {
        loginModel.getUserByAccount(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseBean<List<User>>>() {
                    @Override
                    public void accept(BaseBean<List<User>> listBaseBean) throws Throwable {
//                        Log.i("LoginActivity",listBaseBean.toString());
                        loginView.getUserByAccountSuccess(listBaseBean.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {
//                        Log.e("LoginActivity",throwable.toString());
                        loginView.getUserByAccountError(throwable);
                    }
                });
    }
}
